package com.fone.player.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.fone.player.util.RoundImageUtil;

public class RoundImageView extends ImageView {

	public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

	}

	public RoundImageView(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	public RoundImageView(Context context) {
		super(context);

	}

	@SuppressWarnings("deprecation")
	@Override
	public void setImageDrawable(Drawable drawable) {
		if (drawable != null) {
			BitmapDrawable bd;
			if (drawable instanceof TransitionDrawable) {
				Drawable d;
				d = ((TransitionDrawable) drawable).getDrawable(1);
				bd = (BitmapDrawable) d;

				Bitmap bm = bd.getBitmap();

				Drawable mQuickPlayBg = new BitmapDrawable(
						RoundImageUtil.toRoundBitmap(bm));
				setBackgroundDrawable(mQuickPlayBg);
			} else if (drawable instanceof ColorDrawable) {
				super.setImageDrawable(drawable);
			} else if (drawable instanceof Drawable) {
				try {
					bd = (BitmapDrawable) drawable;
					Bitmap bm = bd.getBitmap();
					Drawable mQuickPlayBg = new BitmapDrawable(
							RoundImageUtil.toRoundBitmap(bm));
					setBackgroundDrawable(mQuickPlayBg);
				} catch (Exception e) {
					// L.i("RoundImageView", "setImageDrawable1  e:",
					// e.toString()+"");
					// try {
					// setImageDrawable(drawable);
					// } catch (Exception e1) {
					// L.i("RoundImageView", "setImageDrawable2  e1:",
					// e1.toString()+"");
					// }
				}
			}
		}
	}

	@Override
	public void setImageBitmap(Bitmap bm) {
		if (bm != null) {
			@SuppressWarnings("deprecation")
			Drawable mQuickPlayBg = new BitmapDrawable(
					RoundImageUtil.toRoundBitmap(bm));
			setBackgroundDrawable(mQuickPlayBg);
		}

	}

}
